﻿using Resume.IRepository;
using Resume.Model;
using Resume.ModelMigration;
using System.Linq.Expressions;

namespace Resume.Repository;

public class FieldClassDict_Repository : IFieldClassDict_Repository
{
    private static XLZFContext context = new XLZFContext();

    private readonly IRepository<FieldClassDict> _repository = new Repository<FieldClassDict>(context);

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="FieldClassDict"></param>
    /// <returns></returns>
    public int Add(FieldClassDict FieldClassDict)
    {
        return _repository.Add(FieldClassDict);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="FieldClassDict"></param>
    /// <returns></returns>
    public int Del(FieldClassDict FieldClassDict)
    {
        return _repository.Remove(FieldClassDict);
    }

    /// <summary>
    /// 改
    /// </summary>
    /// <param name="FieldClassDict"></param>
    /// <returns></returns>
    public int Edit(FieldClassDict FieldClassDict)
    {
        return _repository.Update(FieldClassDict);
    }

    /// <summary>
    /// 查
    /// </summary>
    /// <returns></returns>
    public List<FieldClassDict> Get()
    {
        return _repository.GetAll().ToList();
    }

    /// <summary>
    /// 查
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public FieldClassDict GetSinge(int id)
    {
        return _repository.GetModel(p => p.Id == id);
    }

    /// <summary>
    /// 条件查询
    /// </summary>
    /// <param name="name">姓名</param>
    /// <param name="isAsc">是否正序</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">每页大小</param>
    /// <returns></returns>
    public Page<FieldClassDict> GetPageData(string name, bool isAsc, int pageIndex, int pageSize)
    {
        Page<FieldClassDict>? data = null;

        if (string.IsNullOrEmpty(name))
        {
            data = _repository.SearchFor(pageIndex, pageSize, null, isAsc, e => e.CreateDate);
        }
        else
        {
            //条件表达式
            Expression<Func<FieldClassDict, bool>> predicate = e => string.IsNullOrWhiteSpace(name) || e.FieldClassName.Equals(name);

            data = _repository.SearchFor(pageIndex, pageSize, predicate, isAsc, e => e.CreateDate);
        }

        return data;
    }
}